package com.admin.system.service;

import java.util.List;

import com.admin.base.Page;
import com.admin.system.common.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.system.auth.entity.ShiroUser;
import com.admin.system.dao.SystemUserMapper;
import com.admin.system.entity.SystemUser;
import com.admin.system.vo.query.SystemUserQuery;

@Service
public class SystemUserService {
    @Autowired
    SystemUserMapper userMapper;
    @Autowired
    SystemGroupService groupService;

    public SystemUser queryById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页查询
     *
     * @return
     */
    public Page<SystemUser> paging(SystemUserQuery query) {
        if (query.getScope().equals(SystemConstant.GroupScope.ALL.getCode())) {
            List<Integer> ids = groupService.getChildList(query.getGroupId());
            query.setGroupIds(ids);
        }
        return userMapper.paging(query);
    }

    public Integer pageCount(SystemUserQuery query) {
        Integer count = userMapper.pcount(query);
        return count;
    }

    public void add(SystemUser record) {
        userMapper.insertSelective(record);
    }

    public void delete(Integer[] ids) {
        for (Integer id : ids) {
            userMapper.deleteByPrimaryKey(id);
        }
    }

    public void update(SystemUser record) {
        userMapper.updateByPrimaryKeySelective(record);
    }

    public void updateStatus(SystemUser user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    public SystemUser queryByUserName(String userName) {
        return userMapper.queryByUserName(userName);
    }

    public ShiroUser queryShiroUser(Integer userId) {
        return userMapper.queryShiroUser(userId);
    }

}